package com.ygqh.baby.service.impl;
import java.util.List;
import java.util.Map;

import  com.ygqh.baby.ao.QueryInfo;
import  com.ygqh.baby.ao.ResultSet;
import  com.ygqh.baby.po.YgSearchKeywords;
import  com.ygqh.baby.mapper.YgSearchKeywordsMapper;
import  com.ygqh.baby.service.YgSearchKeywordsService;
import  org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import  org.springframework.stereotype.Service;
import  org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;

import  com.ygqh.baby.po.YgSearchKeywordsExample;
import com.ygqh.baby.redis.RedisDao;
import  com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.KeyWordsStatus;

@Service
public class YgSearchKeywordsServiceImpl implements YgSearchKeywordsService{
	
	@Autowired
	private YgSearchKeywordsMapper ygSearchKeywordsMapper;
	

	
	@Override
	public List<YgSearchKeywords> find() {
		YgSearchKeywordsExample example = new YgSearchKeywordsExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygSearchKeywordsMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgSearchKeywordsExample example = new YgSearchKeywordsExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygSearchKeywordsMapper.countByExample(example);
	}

	@Override
	public YgSearchKeywords findById(Long id) {
		return ygSearchKeywordsMapper.selectByPrimaryKey(id);
	}


	

	@Override
	public int save(YgSearchKeywords ygSearchKeywords) {
		return ygSearchKeywordsMapper.insertSelective(ygSearchKeywords);
	}

	@Override
	public int update(YgSearchKeywords ygSearchKeywords) {
		return ygSearchKeywordsMapper.updateByPrimaryKeySelective(ygSearchKeywords);
	}

	@Override
	public ResultSet<YgSearchKeywords> search(QueryInfo queryInfo,String q) {
		List<YgSearchKeywords>  list =  ygSearchKeywordsMapper.selectSuper(queryInfo, q);
		int count = ygSearchKeywordsMapper.countSuper(q);
		ResultSet<YgSearchKeywords> resultSet = new ResultSet<YgSearchKeywords>(count,list);
		return resultSet;
	}
	
	@Override
	public int updateByExampleSelectiveStatus(YgSearchKeywords ygSearchKeywords) {
		return ygSearchKeywordsMapper.updateByPrimaryKeySelectiveStatus(ygSearchKeywords);
	}	

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgSearchKeywordsExample example = new YgSearchKeywordsExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgSearchKeywords record = new YgSearchKeywords();
		record.setStatus(KeyWordsStatus.hide);
		return ygSearchKeywordsMapper.updateByExampleSelective(record, example);
	}
	
	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[]{id});
	}

	@Override
	public int saveOrUpdate(YgSearchKeywords ygSearchKeywords) {
		int r = 0;
		if(ygSearchKeywords.getId() != null) {
			r = this.update(ygSearchKeywords);
		}else {
			r = this.save(ygSearchKeywords);
		}
		return r;
	}



	@Cacheable(cacheNames="redis",key = "'product:keywords'")
	@Override
	public Map<String,List<String>> findSearchKeywords() {
		Map<String, List<String>> map = new HashMap<String,List<String>>();
		List<YgSearchKeywords> searchkeywordsList = ygSearchKeywordsMapper.findShowModelByKeywords();
		//默认搜索词
		List<String> recommendKeywords=new ArrayList<>();
		//推荐热搜索词
		List<String> searchKeywords = new ArrayList<String>();		
		for(YgSearchKeywords key:searchkeywordsList){
			if(key.getStatus().equals(KeyWordsStatus.defaul)){
				recommendKeywords.add(key.getKeywords());
			}else{
				searchKeywords.add(key.getKeywords());
			}
		}
		
		
		
		map.put("searchKeywords", searchKeywords);
		map.put("recommendKeywords", recommendKeywords);
		return map;
	}



	@Override
	public int findByCount() {
		return ygSearchKeywordsMapper.countByStatus();
	}









}

